local skel = fk.CreateSkill {
  name = "rmt__fuan",
}

skel:addEffect(fk.GameStart, {
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryRound)
  end,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "rmt__jianwang|rmt__jianwangg")
    player:setSkillUseHistory("rmt__fuan", 0, Player.HistoryRound)
  end,
})

skel:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return target == player and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
      and player:usedSkillTimes(skel.name, Player.HistoryRound) == 0
      and not table.contains(data.card.skillNames, skel.name) -- 防止技能造成的延时使用再触发本技能
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name, prompt = "#rmt__fuan-invoke:::"..data.card:toLogString(),
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "@$rmt__fuan-turn", data.card.name)
    if data.toCard then
      data.toCard = nil
    else
      data.tos = {}
    end
    -- 非常麻烦的移动回原处
    local ids = room:getSubcardsByRule(data.card, {Card.Processing})
    if #ids == 0 then return end
    local currentEvent = room.logic:getCurrentEvent()
    local currentUse = currentEvent:findParent(GameEvent.UseCard)
    if not currentUse then return end
    local moveMap = {}
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      if e.parent == currentUse then
        for _, move in ipairs(e.data) do
          if move.toArea == Card.Processing and move.moveReason == fk.ReasonUse
          and move.from and not move.from.dead then
            for _, info in ipairs(move.moveInfo) do
              if table.removeOne(ids, info.cardId) then
                moveMap[move.from] = moveMap[move.from] or {}
                table.insert(moveMap[move.from], info.cardId)
              end
            end
          end
        end
      end
    end, currentUse.id)
    if next(moveMap) ~= nil then
      local moves = {}
      for from, moveIds in pairs(moveMap) do
        table.insert(moves, {
          to = from,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          moveVisible = true,
          ids = moveIds,
        })
      end
      room:moveCards(table.unpack(moves))
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@$rmt__fuan-turn") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@$rmt__fuan-turn")
    room:setPlayerMark(player, "@$rmt__fuan-turn", 0)
    for _, name in ipairs(mark) do
      if player.dead then break end
      room:askToUseVirtualCard(player, {
        name = name, skill_name = skel.name, cancelable = false, extra_data = {bypass_times = true}
      })
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__fuan"] = "伏案",
  [":rmt__fuan"] = "游戏开始时，你获得两个独立计算的技能“鉴往”。每轮限一次，你使用基本牌或普通锦囊牌时，可以取消并移回原处，本回合结束后视为使用之。",

  ["#rmt__fuan-invoke"] = "伏案：你可以取消使用%arg，回合结束时视为使用",
  ["@$rmt__fuan-turn"] = "伏案",

  ["$rmt__fuan1"] = "外人亦言，王欲害我，我岂信之？",
  ["$rmt__fuan2"] = "竖子若过今日，遂不足制也。",
  ["$rmt__fuan3"] = "天命若在，天柱宜自取之。",
}

return skel
